//
//  GlobalSectionHeader.swift
//  ZYA
//
//  Created by 邱仙凯 on 2021/12/7.
//  Copyright © 2021 aowang. All rights reserved.


enum SectionRoundMode {
    //  上面固定圆角，下面根据不同情况配置
    //  ajustConfig  适应配置菜单 开始方角，收起时圆角
    //  alwaysRound 总是圆角
 
    case ajustConfig,alwaysRound
}

import UIKit
 let TempDetBg = UIColor(kshex:"#f4faff")
 
class GlobalSectionHeader:UITableViewHeaderFooterView {
    
    var deleteClo:(()->())?
    lazy var blockV:UIView = {
       let v = UIView()
        v.backgroundColor = UIColor.blue
        return v
    }()
    lazy var lineV:UIView = {
       let v = UIView()
        v.backgroundColor = UIColor.init(kshex: "eeeeee")
        return v
    }()
    private lazy var titleLab:UILabel = {
       let v = UILabel()
        v.numberOfLines = 0
        v.font = UIFont.boldSystemFont(ofSize: 16)
        v.textColor = .black
        return v
    }()
    var delBtnHeight:CGFloat = 21
    lazy var delBtn:UIButton = {
       let v = UIButton()
        v.setTitle("删除", for: .normal)
        v.layer.borderWidth = 1
        v.layer.borderColor = UIColor(kshex: "FF4A4A").cgColor
        v.layer.cornerRadius = delBtnHeight/2
        v.titleLabel?.font = UIFont.systemFont(ofSize: 11)
        v.setTitleColor(UIColor(kshex: "FF4A4A"), for: .normal)
        return v
    }()
   private  lazy var foldBtn:UIButton = {
       let v = UIButton()
       v.isUserInteractionEnabled = false
       v.setBackgroundImage(UIImage(named: "h_zhankai"), for: .normal)

       
  
         return v
    }()
    lazy var bottombg:UIView = {
        let v = UIView()
        v.backgroundColor = .white
 
         return v
    }()
    lazy var bg:UIView = {
        let v = UIView()
        
        v.backgroundColor = TempDetBg
        
        
        let t_left_top = CornerView(width: 8, outColor: .white, innerColor: TempDetBg, type: .leftTop)
        v.addSubview(t_left_top)
        t_left_top.snp.makeConstraints { make in
            make.left.top.equalTo(v)
             make.width.height.equalTo(8)
        }
        let t_right_top = CornerView(width: 8, outColor: .white, innerColor: TempDetBg, type: .rightTop)
        v.addSubview(t_right_top)
        t_right_top.snp.makeConstraints { make in
            make.right.top.equalTo(v)
             make.width.height.equalTo(8)
        }
        v.addSubview(roundleftB_lightblue)
        roundleftB_lightblue.snp.makeConstraints { make in
            make.left.bottom.equalTo(v)
             make.width.height.equalTo(8)
        }
        v.addSubview(roundRightB_lightblue)
        roundRightB_lightblue.snp.makeConstraints { make in
            make.right.bottom.equalTo(v)
             make.width.height.equalTo(8)
        }
        
        
         return v
    }()
    lazy var roundleftB_lightblue:UIView = {
        let v = CornerView(width: 8, outColor: .white, innerColor: TempDetBg, type: .leftBottom)
        v.isHidden = true
        return v
    }()
    lazy var roundRightB_lightblue:UIView = {
        let v = CornerView(width: 8, outColor: .white, innerColor: TempDetBg, type: .rightBottom)
        v.isHidden = true
        return v
    }()
    func setTitle(_ str:String?) {
        titleLab.text = str
        if str.unwrap.count > 20 {
            titleLab.font = UIFont.boldSystemFont(ofSize: 14)
        } else {
            titleLab.font = UIFont.boldSystemFont(ofSize: 16)
        }
    }

    
    var roundMode:SectionRoundMode = .ajustConfig {
        didSet {
             if roundMode == .alwaysRound {
                roundleftB_lightblue.isHidden = false
                roundRightB_lightblue.isHidden = false
            }
         }
        
    }
     // 展开和收起
    func setOpenx(flag:Bool) {
        if flag {
            foldBtn.setBackgroundImage(UIImage(named: "h_shouqi"), for: .normal)
            lineV.isHidden = false
            
            if roundMode == .ajustConfig {
                roundleftB_lightblue.isHidden = true
                roundRightB_lightblue.isHidden = true
            }
           
        } else {
            foldBtn.setBackgroundImage(UIImage(named: "h_zhankai"), for: .normal)
             lineV.isHidden = true
            roundleftB_lightblue.isHidden = false
            roundRightB_lightblue.isHidden = false
        }
    }
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        
        self.contentView.backgroundColor = Theme.awModuleBgColor
        contentView.addSubview(bottombg)
   
       
        bottombg.addSubview(bg)
        bottombg.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(6)
            make.right.equalToSuperview().offset(-6)
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        bg.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalToSuperview().offset(13)
            make.bottom.equalToSuperview().offset(0)
        }
        bg.addSubview(blockV)
        bg.addSubview(titleLab)
        bg.addSubview(delBtn)
        bg.addSubview(foldBtn)
        bg.addSubview(lineV)
        blockV.snp.makeConstraints { make in
          make.left.equalToSuperview().offset(5)
            make.width.equalTo(3)
            make.height.equalTo(15)
            make.centerY.equalToSuperview()
      }
        lineV.snp.makeConstraints { make in
          make.left.equalToSuperview().offset(8)
            make.right.equalToSuperview().offset(-8)
            make.height.equalTo(1)
            make.bottom.equalToSuperview()
      }
        titleLab.snp.makeConstraints { make in
             make.left.equalTo(blockV.snp.right).offset(5)
             make.right.equalToSuperview().offset(-60)
           make.centerY.equalToSuperview()
       }
        delBtn.snp.makeConstraints { make in
            make.right.equalTo(foldBtn.snp.left).offset(-8)
            make.centerY.equalToSuperview()
            make.width.equalTo(47)
            make.height.equalTo(delBtnHeight)
        }
        delBtn.addTouchUpInSideAction{ _ in
            if let clo = self.deleteClo {
                clo()
            }
        }
        foldBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-8)
            make.centerY.equalToSuperview()
            make.width.equalTo(47)
            make.height.equalTo(delBtnHeight)
        }
        
    }
  
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
